﻿namespace com.crownpack
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;

    using com.crownpack.Extensions.SharePointExtensions;
    using com.crownpack.InitiativesDao;
    using com.crownpack.SharePoint;

    using Microsoft.SharePoint;

    using CustomContentType = Final_Definition_SiteLevel;

    public class InitiativesList_Definition_WebLevel : CustomBaseDefinition
    {
        #region Constructors

        public InitiativesList_Definition_WebLevel( SPFeatureReceiverProperties properties )
            : base(properties)
        {
        }

        ~InitiativesList_Definition_WebLevel( )
        {
            Dispose( false );
        }

        #endregion Constructors

        #region Properties

        public ListDefn Initiatives
        {
            get
            {
                return new ListDefn
                {
                    RootFolderName = "InitiativesL",
                    Title = "Initiatives",
                    Description = "Contains initiatives.",
                    ContentTypes = new string[ ]
                    {
                        CustomContentType.CostAvoidanceImplemented.Name,
                        CustomContentType.CostAvoidanceInProcess.Name,
                        CustomContentType.CostAvoidanceProposed.Name,
                        CustomContentType.CostReductionImplemented.Name,
                        CustomContentType.CostReductionInProcess.Name,
                        CustomContentType.CostReductionProposed.Name,
                        CustomContentType.CustomerProvidedImplemented.Name,
                        CustomContentType.CustomerProvidedInProcess.Name,
                        CustomContentType.CustomerProvidedProposed.Name,
                        CustomContentType.ProductRedesignImplemented.Name,
                        CustomContentType.ProductRedesignInProcess.Name,
                        CustomContentType.ProductRedesignProposed.Name,
                        //CustomContentType.UnknownInitiative.Name,
                    }.Distinct( ).ToList( )
                };
            }
        }

        #endregion Properties

        #region Methods

        public override void Create( )
        {
            base.Create( );
            CreateList( Initiatives );
            CreateViews( );
            addEventReceiver( );
            addConfiguration( );
        }

        public override void Delete( )
        {
            base.Delete( );
            deleteConfiguration( );
            DeleteList( Initiatives, false );
        }

        void addConfiguration( )
        {
            string title = string.Empty;
            string url = string.Empty;
            using ( SPWeb spWeb = Properties.GetWeb( ) )
            {
                title = spWeb.ParentWeb.Title;
                url = spWeb.ParentWeb.Url;
            }

            InitiativesConfigurationDao dao = new InitiativesConfigurationDao( Properties.GetWebUrl( ) );
            dao.Add( new InitiativesConfiguration
            {
                Active = false,
                ClientSiteUrl = url,
                ClientSiteDescription = title,
                ParentCustomerNo = null,
                SourceList = string.Empty,
                Title = title
            } );
        }

        void addEventReceiver( )
        {
            string assembly = Assembly.GetAssembly( typeof( InitiativesItemEventReceiver ) ).FullName;
            string className = typeof( InitiativesItemEventReceiver ).FullName;

            using ( SPWeb spWeb = Properties.GetWeb( ) )
            {
                SPList spList = spWeb.Lists.GetListByRootFolder( Initiatives.RootFolderName );

                List<SPEventReceiverDefinition> l = new List<SPEventReceiverDefinition>( );
                foreach ( SPEventReceiverDefinition v in spList.EventReceivers )
                    if ( ( v.Type == SPEventReceiverType.ItemAdded ||
                        v.Type == SPEventReceiverType.ItemDeleting ||
                        v.Type == SPEventReceiverType.ItemUpdated ) &&
                        v.Assembly.Equals( assembly ) && v.Class.Equals( className ) )
                        l.Add( v );
                for ( int i = l.Count - 1; i >= 0; i-- )
                    l[ i ].Delete( );

                spList.EventReceivers.Add( SPEventReceiverType.ItemAdded, assembly, className );
                spList.EventReceivers.Add( SPEventReceiverType.ItemDeleting, assembly, className );
                spList.EventReceivers.Add( SPEventReceiverType.ItemUpdated, assembly, className );
            }
        }

        void createView( string schemaName, string viewUrl, string viewTitle )
        {
            createView( schemaName, viewUrl, viewTitle, false );
        }

        void createView( string schemaName, string viewUrl, string viewTitle, bool defaultView, SPViewCollection.SPViewType viewType )
        {
            using ( SPWeb spWeb = Properties.GetWeb( ) )
            {
                SPList spList = spWeb.Lists.GetListByRootFolder( Initiatives.RootFolderName );
                string schema = GetViewSchemaAsXmlString( schemaName );
                SPView spView = spList.CreateView( viewUrl, viewTitle, 300, true, defaultView, viewType, schema, true );
                if ( viewType == SPViewCollection.SPViewType.Html )
                {
                    spView.ApplyStyle( spWeb.ViewStyles.StyleByID( VIEWSTYLES.Shaded ) );
                    spView.Update( );
                }
            }
        }

        void createView( string schemaName, string viewUrl, string viewTitle, bool defaultView )
        {
            createView( schemaName, viewUrl, viewTitle, defaultView, SPViewCollection.SPViewType.Html );
        }

        void CreateViews( )
        {
            createView( "Views.Client.ActiveInitiatives.xml", "ActiveInitiatives", "Active Initiatives" );
            createView( "Views.Client.ArchivedInitiatives.xml", "Archived Initiatives", "Archived Initiatives" );
            createView( "Views.Client.ByDivision.xml", "Division", "By Division" );
            createView( "Views.Client.ByLocation.xml", "Location", "By Location" );
            createView( "Views.Client.ByProductCategory.xml", "ProductCategory", "By Product Category" );
            createView( "Views.Client.BySalesRep.xml", "SalesRep", "By Sales Rep" );
            createView( "Views.Client.BySalesRepWithManager.xml", "By Sales Rep with Manager", "By Sales Rep with Manager" );
            createView( "Views.Client.ByStatus.xml", "Status", "By Status", true );
            createView( "Views.Client.ByStatusWithLocationNumber.xml", "ByStatusWithLocationNumber", "By Status w/ Location Number" );
            createView( "Views.Client.HighLevel.xml", "HighLevel", "High Level" );
            createView( "Views.Client.ImpJDEConfirmed.xml", "Imp  JDE Confirmed", "Imp. - JDE Confirmed", false, SPViewCollection.SPViewType.Grid );
            createView( "Views.Client.ImpNotJDEConfirmed.xml", "Imp  NOT JDE Confirmed", "Imp. - NOT JDE Confirmed", false, SPViewCollection.SPViewType.Grid );
            createView( "Views.Client.ImplementedInitiatives.xml", "Implemented", "Implemented Initiatives" );
            createView( "Views.Client.PastRealizationDate.xml", "PastRealizationDate", "Past Realization Date", false, SPViewCollection.SPViewType.Grid );
            createView( "Views.Client.RejectedInitiatives.xml", "Rejected", "Rejected Initiatives" );
            //createView( "Views.Client.RssView.xml", "rssview", "RssView" );
            createView( "Views.Client.StalledInitiatives.xml", "StalledInitiatives", "Stalled Initiatives", false, SPViewCollection.SPViewType.Grid );
        }

        void deleteConfiguration( )
        {
            string url = string.Empty;
            using ( SPWeb spWeb = Properties.GetWeb( ) )
            {
                url = spWeb.ParentWeb.Url;
            }
            if ( !string.IsNullOrEmpty( url ) )
            {
                InitiativesConfigurationDao dao = new InitiativesConfigurationDao( Properties.GetWebUrl( ) );
                dao.DeleteByClientSite( url );
            }
        }

        #endregion Methods

        #region Nested Types

        public class VIEWSTYLES
        {
            #region Fields

            public const int Shaded = 17;

            #endregion Fields
        }

        #endregion Nested Types
    }
}